//bower所需 git 添加全局路径 >>  set PATH=%PATH%;C:\Program Files\Git\bin
// 地址 项目信息设置
var KEN         = { // 项目设置
	    //"project": "htx_public_wx"
	    "project": "project/index"
	    //,"IP": "wx.heytongxue.com"
	    //,"IP": "girl.heytongxue.com"

    },
    url         = { // 地址设置

	    // 生产环境
	    "css"  : "duct/source/css",
	    "scss" : "duct/scss",
	    "es6"  : "duct/es6",
	    "js"   : "duct/source/js",
	    "img"  : "duct/source/img",
	    "maps" : "duct/source/maps",
	    "other_JS" : "duct/es6/plugin",
	    "html" : KEN.project + "/html",

	    // 发布环境
	    "Dist": KEN.project + "/source",
	    "Css" : KEN.project + "/source/css",
	    "Js"  : KEN.project + "/source/js",
	    "Map" : KEN.project + "/source/maps",
	    "Img" : KEN.project + "/source/img",
	    "Html": KEN.project + "/source/index"
    },
    gulp        = require('gulp'),
    plugins     = require('gulp-load-plugins')(),
    browserSync = require('browser-sync').create(),
    reload      = browserSync.reload;


// 清除文件 组件
(function () {
	gulp.task('clean_dist', function () {
		return gulp.src(url.Dist, {
				read: false
			})
			.pipe(plugins.clean({
				force: true
			}));
	});
	gulp.task('clean_css', function () {
		return gulp.src(url.css, {
				read: false
			})
			.pipe(plugins.clean({
				force: true
			}));
	});
	gulp.task('clean_maps', function () {
		return gulp.src(url.maps, {
				read: false
			})
			.pipe(plugins.clean({
				force: true
			}));
	});
	gulp.task('clean_js', function () {
		return gulp.src(url.js, {
				read: false
			})
			.pipe(plugins.clean({
				force: true
			}));
	});
	gulp.task('_Clean_all', [
		'clean_dist', 'clean_css', 'clean_maps', 'clean_js'
	]);
}());

// 优化文件
(function () {
	// 清除缓存
	gulp.task('clear_cache', function (done) {
		return plugins.cache.clearAll(done);
	});
	// 压缩文件
	(function () {
		// cache读取缓存，压缩Img
		gulp.task('min_img', function () {
			return gulp.src(url.img + '/*.{png,jpg,gif,ico}')
				.pipe(plugins.cache(plugins.imagemin({
					optimizationLevel: 5,
					progressive      : true,
					interlaced       : true
				})))
				.pipe(gulp.dest(url.Img));
		});
		// cache读取缓存，压缩CSS
		gulp.task('min_css', function () {
			return gulp.src(url.css + '/all.css')
				.pipe(plugins.cache(plugins.cssnano()))
				.pipe(gulp.dest(url.Css));
		});
		// cache读取缓存，压缩JS
		gulp.task('min_js', function () {
			return gulp.src(url.js + '/{sea*.js,}')
				.pipe(plugins.cache(plugins.uglify()))
				.pipe(gulp.dest(url.Js));
		});
		// cache读取缓存，压缩HTML
		gulp.task('min_html', function () {
			gulp.src(url.html + '/*.html')
				.pipe(plugins.cache(plugins.htmlmin({
					collapseWhitespace: true // 文件压缩
				})))
				.pipe(gulp.dest(url.Html));
		});
		// 压缩全部文件
		gulp.task('min_all', [
			'min_img',
			'min_css',
			'min_js',
			//'min_html'
		]);
	}());

	// 合并文件
	(function () {
		// 合并CSS
		gulp.task('concat_css', function () {
			gulp.src(url.css + '/*.css')
				.pipe(plugins.concat('all.css'))
				.pipe(gulp.dest(url.css));
		});
		// 合并JS
		gulp.task( 'seaCombo', function(){
			return gulp.src(url.js+"/main.js")
				.pipe( plugins.seajsCombo({
					map : {
						'duct/source/js/main' : './main'
					}
				}))
				.pipe(plugins.concat('seaMain.js'))
				.pipe( gulp.dest(url.js));
		});
		gulp.task('concat_js', function () {
			gulp.src(url.js + '/*.js')
				.pipe(plugins.concat('all.js'))
				.pipe(gulp.dest(url.js));
		});

		// 合并全部
		gulp.task('_Concat_all', [
			'concat_css',
			'seaCombo'
		])
	}());
}());

// 开发环境
(function () {
	// js
	gulp.task("es6", function () {
		return gulp.src(url.es6 + '/*.es6')
			.pipe(plugins.plumber())
			.pipe(plugins.sourcemaps.init({
				loadMaps: true
			}))
			.pipe(plugins.babel({
				"presets": ["es2015"]
			}))
			.pipe(plugins.sourcemaps.write("../maps/js_maps"))
			.pipe(gulp.dest(url.js));


	})
	// 复制必要js
	gulp.task("other_JS", function () {
		return gulp.src(url.other_JS + '/*.js')
			.pipe(gulp.dest(url.js));
	})
	// sass编译
	gulp.task('sass', function () {
		return gulp.src(url.scss + '/*.{scss,sass}')
			.pipe(plugins.plumber())
			.pipe(plugins.sourcemaps.init({
				loadMaps: true
			}))
			.pipe(plugins.sass().on('error', plugins.sass.logError))
			.pipe(plugins.autoprefixer({
				browsers: ['last 5 version', 'safari 5', 'ie 8', 'ie 9', 'firefox 4', 'opera 12.1', 'ios 6', 'android 4'],
				cascade : false
			}))
			.pipe(plugins.sourcemaps.write("../maps/css_maps"))
			.pipe(gulp.dest(url.css));
	});

	// 浏览器预览
	gulp.task('serve', ['sass', 'es6'], function () {
		if (KEN.IP == null) {
			browserSync.init({ // ip or 域名
				server: {
					baseDir: KEN.project
				}
			});
			gulp.watch(KEN.project + "/*.html").on('change', reload);
		} else {
			browserSync.init({ // ip or 域名
				proxy: KEN.IP
			});
			gulp.watch(KEN.IP + "/*").on('change', reload);
		}
		gulp.watch(url.es6 + "/*.{js,es6}").on('change', reload);
		gulp.watch(url.js + "/*.js").on('change', reload);
		gulp.watch(url.css + "/*.css").on('change', reload);
	});

	// 监听文件
	gulp.task('watch', function () {
		gulp.watch(url.scss + '/*.{scss,sass}', ['sass']);
		gulp.watch(url.scss + '/*/*.{scss,sass}', ['sass']);
		gulp.watch(url.es6 + '/*.{js,es6}', ['es6','other_JS']);

	});

	gulp.task('_Watch', [
		'serve', // 打开服务器
		'watch' // 监听文件
	]);
	gulp.task('_Duct', ['sass', 'es6', 'other_JS']);
}());

// 发布环境
(function () {
	// 合并CSS
	gulp.task('_Dist', plugins.sequence('_Concat_all', 'min_all'));

}());

/********* 帮助文档 **********/
gulp.task('help', function () {
	console.log('	gulp help			gulp参数说明');

	console.log('	gulp plugin			插件介绍');

	console.log('	gulp -dist			发布环境');

	console.log('	gulp -duct			开发环境');
});

gulp.task('plugin', function () {

	console.log('	browser-sync			浏览器无刷新测试');

	console.log('   gulp-autoprefixer		添加浏览器前缀');

	console.log('	gulp-concat			    合并文件');

	console.log('	gulp-cssnano			CSS压缩');

	console.log('	gulp-htmlmin			HTML压缩');

	console.log('	gulp-load-plugins		简化配置文件,用(plugins. + 驼峰式插件名)调用');

	console.log('	gulp-imagemin			压缩图片启动器');

	console.log('	gulp-plumber			错误处理器');

	console.log('	gulp-rename			    修改文件名 OR 路径');

	console.log('	gulp-sass			    编译SASS or SCSS');

	console.log('	gulp-sequence			调整程序处理顺序');

	console.log('	gulp-sourcemaps			文件地图');

	console.log('	gulp-uglify			    JS压缩');

	console.log('	gulp-clean			    删除文件夹');

});